package statistics;

import java.util.Random;


public class NegativeBinomialDistribution extends Distribution{

    protected double p;
    protected int r;
    
    public NegativeBinomialDistribution( int r , double p, Random random ){
        this.p = p;
        this.r = r;
        this.random = random;
 
    }
            

    public double expectation() {
       return p*r/(1-p);
    }


    public double variance() {
        return p*r/ ((1-p)*(1-p));
    }


    public double nextRandom() {
        double U;
        int successes=0;
        int failures=0;
        while(failures<r){
            U = random.nextDouble();
            if(U<=p)
                successes++;
            else
                failures++;
            
        }
        return successes;
        
    }
    
    
}